/**
 * Copyright(c) Live2D Inc. All rights reserved.
 *
 * Use of this source code is governed by the Live2D Open Software license
 * that can be found at https://www.live2d.com/eula/live2d-open-software-license-agreement_en.html.
 */

#include "CubismModelUserData.hpp"

#include "CubismModelUserDataJson.hpp"
#include "Utils/CubismString.hpp"

namespace Live2D
{
    namespace Cubism
    {
        namespace Framework
        {

            namespace
            {
                const QString &ArtMesh = "ArtMesh";
            }

            CubismModelUserData::~CubismModelUserData()
            {
                for (auto i = 0; i < _userDataNodes.size(); ++i)
                {
                    CSM_DELETE(const_cast<CubismModelUserDataNode *>(_userDataNodes[i]));
                }
            }

            const QVector<const CubismModelUserData::CubismModelUserDataNode *> &CubismModelUserData::GetArtMeshUserDatas() const
            {
                return _artMeshUserDataNodes;
            }

            CubismModelUserData *CubismModelUserData::Create(const QByteArray &buffer)
            {
                CubismModelUserData *ret = CSM_NEW CubismModelUserData();

                ret->ParseUserData(buffer);

                return ret;
            }

            void CubismModelUserData::Delete(CubismModelUserData *modelUserData)
            {
                CSM_DELETE_SELF(CubismModelUserData, modelUserData);
            }

            void CubismModelUserData::ParseUserData(const QByteArray &buffer)
            {
                CubismModelUserDataJson *json = CSM_NEW CubismModelUserDataJson(buffer);

                const ModelUserDataType typeOfArtMesh = CubismFramework::GetIdManager()->GetId(ArtMesh);

                const csmUint32 nodeCount = json->GetUserDataCount();

                for (csmUint32 i = 0; i < nodeCount; i++)
                {
                    CubismModelUserDataNode *addNode = CSM_NEW CubismModelUserDataNode();
                    addNode->TargetId = json->GetUserDataId(i);
                    addNode->TargetType = CubismFramework::GetIdManager()->GetId(json->GetUserDataTargetType(i));
                    addNode->Value = json->GetUserDataValue(i);
                    _userDataNodes.append(addNode);

                    if (addNode->TargetType == typeOfArtMesh)
                    {
                        _artMeshUserDataNodes.append(addNode);
                    }
                }

                CSM_DELETE(json);
            }
        } // namespace Framework
    }     // namespace Cubism
} // namespace Live2D
